package com.swz.dcrm.ui.main;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.flyco.tablayout.CommonTabLayout;
import com.swz.dcrm.R;
import com.swz.dcrm.adpter.MessageAdapter;
import com.swz.dcrm.model.Announcement;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.WorkRemind;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.ui.viewmodel.AnnouncementViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.ui.viewmodel.TodayPlanViewModel;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.util.DateUtils;
import com.swz.dcrm.util.Tool;
import com.swz.dcrm.wrapper.EmptyWrapper;
import com.xh.baselibrary.widget.BounceScrollView;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class MessageFragment extends BaseFragment {

    @BindView(R.id.scroll)
    BounceScrollView scrollView;
    @BindView(R.id.toolbar)
    ConstraintLayout toolbar;
    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.iv_work)
    ImageView ivWork;
    EmptyWrapper emptyWrapper;

    private MessageAdapter messageAdapter;
    @Inject
    MainViewModel mainViewModel;

    @Inject
    AnnouncementViewModel announcementViewModel;
    @Inject
    TodayPlanViewModel todayPlanViewModel;

    public static MessageFragment getInstance(){
        return new MessageFragment();
    }


    @Override
    public void onLoadRetry() {
        announcementViewModel.findAnnouncement(1,0).observe(this,observer );
        todayPlanViewModel.getWorkRemind().observe(this, workRemindObserver);

    }
    Observer workRemindObserver=new Observer<BaseResponse<List<WorkRemind>>>() {
        @Override
        public void onChanged(BaseResponse<List<WorkRemind>> listBaseResponse) {
            if(listBaseResponse.isSuccess()){
                int count=0;
                if(listBaseResponse.getData().size()>0){
                    for(WorkRemind workRemind:listBaseResponse.getData()){
                        if(DateUtils.dateFormat(workRemind.getTime(),"yyyy-MM-dd").equals(DateUtils.getCurrentDate())){
                            count+=workRemind.getCount();
                        }
                    }
                }
                if(count>0){
                    ivWork.setImageResource(R.mipmap.work_notification_unread);
                }else {
                    ivWork.setImageResource(R.mipmap.work_notification);
                }

                tvSign.setText(getString(R.string.message_not_yet,count));
            }
        }
    };
    Observer observer= (Observer<BaseResponse<Page<Announcement>>>) pageBaseResponse -> {
            if(pageBaseResponse.isSuccess()){
                if(messageAdapter==null){
                    messageAdapter=new MessageAdapter(getContext(),pageBaseResponse.getData().getList());
                    emptyWrapper=getEmptyWrapper(messageAdapter,R.mipmap.empty_notice);
                    rv.setAdapter(emptyWrapper);
                }else {
                    messageAdapter.refresh(1,pageBaseResponse.getData().getList());
                    emptyWrapper.notifyDataSetChanged();
                }
            }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_message;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        toolbar.setAlpha(0);
        rv.setLayoutManager(new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        rv.addItemDecoration(new CustomDecoration(getContext(),0,10,15,15));
        ConstraintLayout.LayoutParams layoutParams= (ConstraintLayout.LayoutParams) toolbar.getLayoutParams();
        layoutParams.height=getStatusBarHeight(getContext())+ Tool.dip2px(getActivity(),50);
        toolbar.setLayoutParams(layoutParams);
        toolbar.setPadding(0,getStatusBarHeight(getContext()),0,0);
        title.setText(getResources().getStringArray(R.array.main_tab_name)[1]);
        scrollView.setOnScrollChangeListener((BounceScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {


            if(scrollY<=tvMessage.getBottom()){

                float y=scrollY;
                float bottom=tvMessage.getBottom();
                toolbar.setAlpha(y/bottom);
            }
        });
        return false;
    }
    @OnClick(R.id.c_message)
    public void workRemind(){
        go(mainViewModel,R.id.action_mainFragment_to_workRemindFragment,null);
    }

}
